package cn.edu.scau.cmi.hexiaoqing.client;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import cn.edu.scau.cmi.hexiaoqing.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.hexiaoqing.composite.consistent.Group;
import cn.edu.scau.cmi.hexiaoqing.composite.consistent.Personal;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ConsistentCompositeClient extends Application {
	private ArrayList<String> groupsList = new ArrayList<String>();
	private Set<Group> groups = new HashSet<Group>();

	@Override
	public void start(Stage primaryStage) throws Exception {
		Stage st1 = new Stage();
		Button bt1 = new Button("New a person");
		Button bt2 = new Button("New a group");
		HBox hb1 = new HBox(30);
		hb1.getChildren().addAll(bt1, bt2);
		hb1.setAlignment(Pos.CENTER);
		getGroupList();

		TextArea ta1 = new TextArea();
		BorderPane bp = new BorderPane();
		bp.setTop(hb1);
		bp.setCenter(getVBox(ta1));
		bp.setBottom(ta1);
		Scene sc1 = new Scene(bp, 500, 500);
		st1.setScene(sc1);
		st1.show();
		bt1.setOnMouseClicked(e -> {
			Text t1 = new Text("Name: ");
			TextField tf1 = new TextField();
			Text t2 = new Text("Group: ");
			getGroupList();
			ChoiceBox<String> cb = new ChoiceBox<String>(FXCollections.observableArrayList(groupsList));
			Button bt = new Button("New");
			GridPane gp = new GridPane();
			gp.setAlignment(Pos.CENTER);
			gp.setHgap(10);
			gp.setVgap(20);
			gp.add(t1, 0, 0);
			gp.add(tf1, 1, 0);
			gp.add(t2, 0, 1);
			gp.add(cb, 1, 1);
			gp.add(bt, 0, 2);
			Scene sc = new Scene(gp, 300, 200);
			Stage st = new Stage();
			st.setScene(sc);
			st.setTitle("New a person");
			st.show();
			bt.setOnMouseClicked(f -> {
				String name = tf1.getText();
				String groupName = cb.getValue();
				if (groupName != null && !"".equals(name)) {
					ConsistentComponent person = new Personal(name);
					for (Group g : groups) {
						if (g.getName().equals(groupName)) {
							g.add(person);
						}
					}
					st.close();
				} else {
					Stage st2 = new Stage();
					GridPane gp1 = new GridPane();
					gp1.setAlignment(Pos.CENTER);
					Text t3 = new Text("Please fill in all the options");
					gp1.add(t3, 0, 0);
					Scene sc2 = new Scene(gp1, 300, 100);
					st2.setScene(sc2);
					st2.setTitle("Error");
					st2.show();
				}
			});
		});
		bt2.setOnMouseClicked(e -> {
			Text t1 = new Text("Name: ");
			TextField tf1 = new TextField();
			Text t2 = new Text("Group(Selective filling): ");
			getGroupList();
			ChoiceBox<String> cb = new ChoiceBox<String>(FXCollections.observableList(groupsList));
			Button bt = new Button("New");
			GridPane gp = new GridPane();
			gp.setAlignment(Pos.CENTER);
			gp.setHgap(10);
			gp.setVgap(20);
			gp.add(t1, 0, 0);
			gp.add(tf1, 1, 0);
			gp.add(t2, 0, 1);
			gp.add(cb, 1, 1);
			gp.add(bt, 0, 2);
			Scene sc = new Scene(gp, 400, 200);
			Stage st = new Stage();
			st.setScene(sc);
			st.setTitle("New a group");
			st.show();
			bt.setOnMouseClicked(f -> {
				String name = tf1.getText();
				String groupName = cb.getValue();
				if (!"".equals(name)) {
					Group group = new Group(name);
					if (groupName != null) {
						for (Group g : groups) {
							if (g.getName().equals(groupName)) {
								g.add(group);
								groups.add(group);
								bp.setCenter(getVBox(ta1));
								break;
							}
						}
					} else {
						groups.add(group);
						bp.setCenter(getVBox(ta1));
					}
					st.close();
				} else {
					Stage st2 = new Stage();
					GridPane gp1 = new GridPane();
					gp1.setAlignment(Pos.CENTER);
					Text t3 = new Text("Please fill in the name");
					gp1.add(t3, 0, 0);
					Scene sc2 = new Scene(gp1, 300, 100);
					st2.setScene(sc2);
					st2.setTitle("Error");
					st2.show();
				}
			});
		});
	}

	public void getGroupList() {
		ArrayList<String> groupsList = new ArrayList<String>();
		for (Group g : groups) {
			groupsList.add(g.getName());
		}
		this.groupsList = groupsList;
	}

	public VBox getVBox(TextArea ta) {
		VBox vb = new VBox(10);
		getGroupList();
		ArrayList<HBox> Hboxes = new ArrayList<HBox>();
		for (Group g : groups) {
			Text t = new Text(g.getName());
			Button bt4 = new Button("Show the information");
			bt4.setOnMouseClicked(e -> {
				ta.setText(g.getInf());
			});
			HBox hb = new HBox(40);
			hb.getChildren().addAll(t, bt4);
			Hboxes.add(hb);
		}

		for (HBox h : Hboxes) {
			vb.getChildren().add(h);
		}
		vb.setPadding(new Insets(30, 0, 15, 30));
		return vb;
	}

	public static void main(String[] args) {
		Application.launch(args);
	}
}
